package com.chengh.nami.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengh.nami.common.domain.pojo.SysDictData;
import com.chengh.nami.common.exception.BizException;
import com.chengh.nami.common.request.PageQuery;
import com.chengh.nami.common.response.PageData;
import com.chengh.nami.common.utils.DictUtils;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.system.mapper.SysDictDataMapper;
import com.chengh.nami.system.service.ISysDictDataService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author chengh
 * @Date 2023-01-02 14:47
 * @Description: 字典数据实现类
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {


    @Override
    public List<SysDictData> getDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (!CollectionUtils.isEmpty(dictDatas)) {
            return dictDatas;
        }
        List<SysDictData> sysDictData = getDictDataByTypeNoCache(dictType);
        if (!CollectionUtils.isEmpty(sysDictData)) {
            DictUtils.setDictCache(dictType, dictDatas);
            return sysDictData;
        }
        return null;
    }

    @Override
    public List<SysDictData> getDictDataByTypeNoCache(String dictType) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<SysDictData>().eq(StringUtils.isNotEmpty(dictType), SysDictData::getDictType, dictType));
    }

    @Override
    public List<SysDictData> getDictDataList() {
        return baseMapper.selectList(null);
    }

    @Override
    public PageData<SysDictData> getList(SysDictData dictData, PageQuery pageQuery) {
        LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<SysDictData>().eq(StringUtils.isNotEmpty(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType())
                .like(StringUtils.isNotEmpty(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel())
                .orderByAsc(SysDictData::getDictSort);
        Page<SysDictData> build = pageQuery.build();
        Page<SysDictData> dictDataPage = baseMapper.selectPage(build, wrapper);
        return PageData.build(dictDataPage);
    }

    @Override
    public Boolean edit(SysDictData dictData) {
        verifyRepeat(dictData);
        int row = baseMapper.updateById(dictData);
        if (row > 0) {
            List<SysDictData> dictDatas = getDictDataByTypeNoCache(dictData.getDictType());
            DictUtils.setDictCache(dictData.getDictType(), dictDatas);
        }
        return row > 0;
    }

    @Override
    public Boolean aaddData(SysDictData dictData) {
        verifyRepeat(dictData);
        int row = baseMapper.insert(dictData);
        if (row > 0) {
            List<SysDictData> dictDatas = getDictDataByTypeNoCache(dictData.getDictType());
            DictUtils.setDictCache(dictData.getDictType(), dictDatas);
        }
        return row > 0;
    }

    @Override
    public Boolean delInfoByIds(List<Long> ids) {
        List<String> types = baseMapper.selectDisType(ids);
        if (CollectionUtils.isEmpty(types)) {
            throw new BizException("根据ID集合查询的数据为空");
        }
        if (types.size() > 1) {
            throw new BizException("根据ID集合查出的字典类型为多个，不允许删除");
        }
        int row = baseMapper.deleteBatchIds(ids);
        if (row > 0) {
            String type = types.get(0);
            List<SysDictData> dictDatas = getDictDataByTypeNoCache(type);
            DictUtils.setDictCache(type, dictDatas);
        }
        return row > 0;
    }

    /**
     * 校验是否重复
     *
     * @param dictData 校验信息
     */
    public void verifyRepeat(SysDictData dictData) {
        LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType, dictData.getDictType())
                .ne(dictData.getId() != null, SysDictData::getId, dictData.getId())
                .eq(SysDictData::getDictValue, dictData.getDictValue());
        Long i = baseMapper.selectCount(wrapper);
        if (i > 0) {
            throw new BizException("字典键值不允许重复");
        }
    }
}
